package cn.wnco.tool;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.Keys;

import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.Map;

import static cn.wnco.conf.Const.jwt_key;

public class JwtTool{

    public static Claims parseToken(String jwt) {
        Jws<Claims> claimsJws = Jwts.parser()
                .verifyWith(Keys.hmacShaKeyFor(jwt_key.getBytes(StandardCharsets.UTF_8)))
                .build()
                .parseSignedClaims(jwt);
        return claimsJws.getPayload();
    }

    public static String createToken(Map<String, Object> map) {
        return Jwts.builder()
                .signWith(Keys.hmacShaKeyFor(jwt_key.getBytes(StandardCharsets.UTF_8)))
                .claims(map)
                .issuedAt(new Date())
                .expiration(new Date(System.currentTimeMillis() + 1000 * 60 * 5))
                .compact();
    }
}